package com.he.observedmodel.subject.concretesubject;

import com.he.observedmodel.observed.Observed;
import com.he.observedmodel.subject.Subject;

import java.util.ArrayList;
import java.util.List;

/**
 * 具体主题类，但主题类的内部状态发生改变时，会通知具体所有观察者
 * @param <T>
 */
public class Concretesubject<T> implements Subject<T> {

    List<Observed> list = new ArrayList<>();

    private T state;

    public List<Observed> getList() {
        return list;
    }

    public void setList(List<Observed> list) {
        this.list = list;
    }

    public T getState() {
        return state;
    }

    @Override
    public void add(Observed observerd) {
        list.add(observerd);
    }

    @Override
    public void delete(Observed observerd) {
        list.remove(observerd);
    }

    @Override
    public void notifyOberved() {
        for (Observed observed : list) {
            observed.update();
        }
    }

    @Override
    public void updateState(T state) {
        this.state = state;
    }

    @Override
    public String toString() {
        return "Concretesubject{" + "'state='" + state + '\'' +
                '}';
    }
}
